package com.bankLogisticsSys.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bankLogisticsSys.model.Expresscontent;
import com.bankLogisticsSys.model.Expressinfo;
import com.bankLogisticsSys.model.Expressprocess;
import com.bankLogisticsSys.model.Expressrecord;
import com.bankLogisticsSys.model.Packageinfo;
import com.bankLogisticsSys.model.Userinfo;
import com.bankLogisticsSys.util.WeixinUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;

//创建客服消息
public class CreateMsgService {
	
	ExpressService exservice = new ExpressService();
	LogisticsService lservice = new LogisticsService();
	
	public String startProcessMsg(int action){
		String msg;
		if(action != 3){
			msg = "开始" +TransformService.getAction(action)+ " ，误操作请忽略此回复\n"
						+ "正常操作请直接开始扫码，系统将自动为您匹配对应的快递单\n"
						+ "<a href=\"http://fiberkenzo.com/weixin/" +TransformService.getActionEN(action)+ "\">"
						+ TransformService.getAction(action) +"</a>";
		}else{
			msg = "开始" +TransformService.getAction(action)+ " ，误操作请忽略此回复\n"
					+ "正常操作请点击下方的链接创建快递单，完成创建即可扫码识别包裹\n"
					+ "<a href=\"http://fiberkenzo.com/weixin/" +TransformService.getActionEN(action)+ "\">"
					+ TransformService.getAction(action) +"</a>";
		}
		return msg;
	}
	
	public String getActionMsg(int trackingID, int action, Userinfo user){
		String msg = "";
		Expressinfo ei = Expressinfo.dao.findFirst("select * from expressinfo where trackingid = ?", trackingID);
		List<Expresscontent> ec = Expresscontent.dao.find("select * from expresscontent where trackingid = ? and bpackageid is null", trackingID);
		List<Expressrecord> er = Expressrecord.dao.find("select * from expressrecord where trackingid = ? and action = ? and employeeid = ?", trackingID, action, user.getEmployeeID());
		msg += "当前操作为： " + TransformService.getAction(action);
		msg += "\n\n   快递单号： " + trackingID;
		msg += "\n   始发网点号： " + ei.getSource();
		msg += "\n   目的网点号： " + ei.getDestination();
		msg += "\n   发件时间： " + ei.getSendDate();
		if(action != 1){
			if(ec.size() != (er.size()+1)){
				msg += "\n   包裹总数： " + ec.size();
				msg += "\n   已扫描包裹： " + (er.size()+1);
				msg += "\n\n查看详情请点击下方的链接";
			}else{
				msg += "\n\n已扫描所有包裹，请点击下方的链接确认";
			}
		} else{
			msg += "\n   已添加包裹数: （封入大包的小包不计数）)" + (ec.size()+1);
			msg += "\n\n完成所有包裹添加后，请点击下方的链接确认";
		}
		msg += "\n\n<a href=\"http://fiberkenzo.com/weixin/" + TransformService.getActionEN(action) + "\">" + TransformService.getAction(action) + "</a>";
		return msg;
	}
	public String getActionMsg2(long trackingID,int sequence, int action, Userinfo user, int abnormal){
		String msg = "";
		String msg2 = "";
		Expressinfo ei = Expressinfo.dao.findFirst("select * from expressinfo where trackingid = ?", trackingID);
		List<Expresscontent> ec = Expresscontent.dao.find("select * from expresscontent where trackingid = ?", trackingID);
		List<Expressrecord> er = Expressrecord.dao.find("select * from expressrecord where trackingid = ? and action = ? and employeeid = ?", trackingID, action, user.getEmployeeID());
		List<Expresscontent> ecB = Expresscontent.dao.find("select * from expresscontent where trackingid = ? and bpackageid is null", trackingID);
		if(abnormal ==1){
			msg += "异常：上一个操作流程可能未扫码确认\n\n";
		}else if(abnormal ==2){
			msg += "异常：包裹运输路径异常\n\n";
		}
		if(user.getNetworkID().equals(ei.getSource())){
			msg2 += "\n   已添加包裹数: " + ec.size();
			msg2 += "\n\n完成所有包裹添加后，请点击下方的链接确认";
			action = 3;
		}else if(user.getNetworkID().equals(ei.getDestination())){
			msg2 += "\n   本单包裹总数： " + ec.size();
			msg2 += "\n   本单已验收包裹数：" + er.size();
			msg2 += "\n\n验收所有的包裹后，请点击下方的链接确认";
			action = 4;
		}else{
			if(ecB.size() != er.size()){
				msg2 += "\n   本单包裹总数： " + ecB.size();
				msg2 += "\n   本单已扫描包裹： " + er.size();
				msg2 += "\n\n查看详情请点击下方的链接";
			}else{
				msg2 += "\n\n已扫描本单所有包裹，请点击下方的链接确认";
			}
		}
		msg += "当前操作为： " + TransformService.getAction(action);
		msg += "\n\n   快递单号： \n   " + trackingID;
		msg += "\n   始发网点： " + TransformService.getNetwork(ei.getSource());
		msg += "\n   目的网点： " + TransformService.getNetwork(ei.getDestination());
		msg += "\n   发件时间： " + ei.getSendDate();
		msg += msg2;
		msg += "\n\n<a href=\"http://fiberkenzo.com/weixin/" + TransformService.getActionEN(action) + "\">" + TransformService.getAction(action) + "</a>";
		return msg;
	}
	
	public String getFinishMsg(int trackingID, int sequence, int action, Userinfo user){
		return "您对快递单：" + trackingID + " 的" + "操作已经完成";
	}
	
	public String isPackageMsg(String packageCode){
		Packageinfo pi = Packageinfo.dao.findFirst("select * from packageinfo where packageCode = ?", packageCode);
		if(isPackage(packageCode)){
			return  "包裹信息:\n" +
					"包裹编号：" + packageCode + 
//					"\n包裹类型：" + TransformService.getPackageType(pi.getType()) +
					"\n包裹业务：" + TransformService.getBusiness(pi.getBusinessID()) +
					"\n所属站点：" + TransformService.getNetwork(pi.getNetworkID());
		}else{
			return "抱歉，无法找到您扫描的包裹，请确认此包裹是否添加到数据库";
		}
	}
	
	//帮助信息
	public String helpMsg(){
		return "银行包裹收发跟踪系统操作指南\n发送 help 可获取帮助信息\n\n"
				+ "发送下方对应的数字获取相应的操作帮助\n"
				+ "1、如何注册和登陆" + "\n" 
				+ "2、如何派单" + "\n" 
				+ "3、如何揽收" + "\n" 
				+ "4、网点接收和发送" + "\n" 
				+ "5、包裹送达与验收" + "\n" 
				+ "6、空包退回" + "\n" 
				+ "7、查询包裹信息" + "\n";
	}
	
	public Boolean isPackage(String packageCode){
		Packageinfo pi = Packageinfo.dao.findFirst("select * from packageinfo where packagecode = ?", packageCode);
		if(pi != null){
			return true;
		}else{
			return false;
		}
	}
	
	public List<Map<String, String>> getExpressLine(List<Expressprocess> list, Expressinfo ei){
		List<Map<String, String>> exList = new ArrayList<Map<String,String>>();
		for(int i=0, size=list.size(); i<size;i++){
			Map<String, String> map = new HashMap<String, String>();
			String msg = "";
			Expressprocess ep = list.get(i);
			msg = lservice.processMsg2(ei.getTrackingID(), ep.getEmployeeID(), ep.getAction(), ep.getSequence());
			map.put("time", ep.getTime().toLocaleString());
			map.put("msg", msg);
			exList.add(map);
		}
		return exList;
	}
	public String oauth() {
		String appId = WeixinUtil.getApiConfig().getAppId();
		String redirectUri = WeixinUtil.urlEncodeUTF8(PropKit.get("domain") + "/oauth2");
		String state = System.currentTimeMillis() + "";

		String url = SnsAccessTokenApi.getAuthorizeURL(appId, redirectUri, state, false);
		return url;
	}
	public String getHelpMsg(String msgContent){
		String outMsg;
		if ("help".equalsIgnoreCase(msgContent) || "帮助".equals(msgContent)) {
			outMsg ="用户您好,欢迎关注银行物流管理公众号 \n\n\t发送 help 可获取帮助信息\n\n"
					+ "银行和邮政员工请点击\n个人中心->登录 绑定账号" + "\n" 
					+ "\n点击\n个人中心->帮助，显示帮助信息" + "\n"
					+ "发送下方对应的数字获取相应的操作帮助\n"
					+ "1、如何注册和登陆" + "\n" 
					+ "2、如何派单" + "\n" 
					+ "3、如何揽收" + "\n" 
					+ "4、网点接收和发送" + "\n" 
					+ "5、包裹送达与验收" + "\n" 
					+ "6、空包退回" + "\n" 
					+ "7、查询包裹信息" + "\n";
		}else if("2".equalsIgnoreCase(msgContent)){
			outMsg ="2、如何派单\n" + 
					"只有银行员工工号才有派单权限。\n" +
					"使用银行员工工号登陆，点击下方物流管理->派单，接收到开始派单的提示信息，点击提示信息中“派单”，进入确认页面，点击“创建快递单”生成一条快递单号。如不确认，直接关闭确认页面，则不会生成快递单号\n。" +
					"生成快递单号后，点击扫码开始扫描要发送的包裹。\n" +
					"扫描包裹时，需先扫描小包，再扫描大包，小包和大包会自动关联。\n" +
					"包裹扫描完毕后，点击提示信息中的“派单”，进入确认页面，派单人员对发送包裹信息进行核对，核对无误后，点击“确认封包”，则生成一条快递单，并推送信息给揽收人员。";
		}else if("1".equalsIgnoreCase(msgContent)){
			outMsg ="1、	如何注册和登陆\n" + 
					"本公众号不对外发布，注册请直接联系管理人员。\n" +
					"点击右下角个人中心->登录，输入工号，密码后确认，即可登陆本公众号。";
		}else if("3".equalsIgnoreCase(msgContent)){
			outMsg = "3、如何揽收\n" +
					"揽收人员上门接收包裹，点击扫码对包裹依次扫码。已封包的大包无需扫描小包。\n"+
					"包裹扫描完毕后，点击提示信息中的“揽收”，进入确认页面，揽投人员对包裹信息进行核对，核对无误后，点击“揽收完成”。\n";
		}else if("4".equalsIgnoreCase(msgContent)){
			outMsg = "4、网点接收和发送\n" +
					"揽收人员将包裹送达网点，网点人员，点击扫码对接收包裹依次扫码。已封包的大包无需扫描小包。\n" +
					"包裹扫描完毕后，点击提示信息中的“接收”，进入确认页面，网点人员对发送包裹信息进行核对，核对无误后，点击“接收完成”。\n" +
					"网点将包裹进行发送出去，网点人员点击扫码对发送包裹依次扫码。\n" +
					"包裹扫描完毕后，点击提示信息中的“发送”，进入确认页面，网点人员对发送包裹信息进行核对，核对无误后，点击“发送完成”。\n";
		}else if("5".equalsIgnoreCase(msgContent)){
			outMsg = "5、包裹送达与验收\n" +
					"投递人员，从网点取包裹，点击扫码对接收包裹依次扫码。\n" +
					"包裹扫描完毕后，点击提示信息中的“揽收”，进入确认页面，揽投人员对包裹信息进行核对，核对无误后，点击“揽收完成”。\n" +
					"包裹送抵客户后，银行员工，对全部包裹进行扫码，包括大包、小包，点击扫码对包裹依次扫码。\n" +
					"包裹扫描完毕后，点击提示信息中的“验收”，进入确认页面，银行员工对包裹信息进行核对，核对无误后，点击“验收完成”。";
		}else if("6".equalsIgnoreCase(msgContent)){
			outMsg = "6、空包退回\n" +
					"银行员工，点击物流管理->空包退回，则会将受到的全部包裹统一生成快递单，由揽收人员对全部包裹扫码确认后，完成空包退回操作。\n";
		}else if("7".equalsIgnoreCase(msgContent)){
			outMsg = "7、查询包裹信息\n" +
					"点击个人中心->包裹识别，扫描包裹，系统会返回该包裹的详细信息。\n";
		}
		// 其它文本消息直接返回原值 + 帮助提示
		else {
			outMsg ="请发送对应的数字或者help,获取操作帮助 \n"
					+ "1、如何注册和登陆" + "\n" 
					+ "2、如何派单" + "\n" 
					+ "3、如何揽收" + "\n" 
					+ "4、网点接收和发送" + "\n" 
					+ "5、包裹送达与验收" + "\n" 
					+ "6、空包退回" + "\n" 
					+ "7、查询包裹信息" + "\n";
		}
		return outMsg;
	}
}
